import config from 'config';
import httpFetch from 'share/httpFetch';
import errorMessage from 'share/errorMessage';
import OrgService from 'containers/enterprise-manage/org-structure/org-structure.service';
export default {
  //导入公司信息模版
  downloadCompanyTemplate: function () {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/company/template/download', {},  {},{responseType: 'arraybuffer'})
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //导入银行信息模版
  downloadBankTemplate: function () {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/CompanyBank/template/download', {},  {},{responseType: 'arraybuffer'})
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //导入公司或银行信息
  importCompanyOrBank: function (file) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/api/company/import', file)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //导入监听返回:公司信息导入之后监听
  getBatchTransactionLogNew: function (transactionOID) {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/batch/transaction/logs/v2/' + transactionOID)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  //导出错误记录：公司信息
  exportFailedLog: function (transactionOID) {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/batch/transaction/logs/failed/export/company/' + transactionOID,{},{},
        {responseType: "arraybuffer"})
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //条件查询公司
  getCompaniesByOptions(params){
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/company/by/term', params)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //根据公司id查询公司信息
  getCompanyById(id){
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/company/' + id)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //根据公司oid，与法人实体id，过滤公司
  getSelectListParentCompany(params){
    // let params = {
    //   legalEntityId:"legalEntityId",
    //   filterCompanyOIDs:"filterCompanyOIDs"
    // }
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/company/by/tenant',params)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //新建公司
  addCompany(company){
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/api/refactor/tenant/company/register', company)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //编辑公司
  updateCompany(company){
    return new Promise((resolve, reject) => {
      httpFetch.put(config.baseUrl + '/api/refactor/companies', company)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //获取银行账户信息
  getBankAccountInfo(params){
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/CompanyBank/selectByCompanyId', params)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //获取员工信息
  getUserInfo(params) {
    return new Promise((resolve, reject) => {
      OrgService.searchPersonPosition(params)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //新建银行账户
  addOrUpdateBankAccount(params){
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/api/CompanyBank/insertOrUpdate', params)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //根据id查询银行信息
  getCompanyBankInfoById(id){
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/CompanyBank/selectById?companyBankId=' + id)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //查询公司信息
  getCompanyInfo(companyOID){
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/companies/' + companyOID)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },
  movePersonToCompany(params){
    // ?companyOIDTo=公司oid&userJobsIds=岗位id集合
    return new Promise((resolve, reject) => {
      httpFetch.put(config.baseUrl + '/api/user/jobs/move/by/company', params)
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  }
}
